#include "setupdlg.h"
#include "ui_setupdlg.h"
#include <qsettings.h>
#include <qfiledialog.h>
#include "qextserialport.h"
#include <QDebug>

SetUpDlg::SetUpDlg(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);
    fillBaudCb();

    readSettings();
    connect(m_logFileFileDialog, SIGNAL(clicked()), this, SLOT(chooseLogFile()));
}

SetUpDlg::~SetUpDlg()
{

}

void SetUpDlg::readSettings()
{
   QSettings settings;
    //qDebug()<<"setting file path is :" << settings.fileName();

   m_hardwareCb->setChecked(settings.value("/serialsetting/HardwareHandshake", false).toBool());
   m_softwareCb->setChecked(settings.value("/serialsetting/SoftwareHandshake", false).toBool());
   m_writeCb->setChecked(settings.value("/serialsetting/OpenForWriting", true).toBool());
   //m_applyCb->setChecked(!settings.value("/serialsetting/DontApplySettings", false).toBool());
   //on_m_applyCb_toggled(m_applyCb->isChecked());

   m_baudCb->setCurrentIndex(m_baudCb->findData(settings.value("/serialsetting/Baud", BAUD115200).toUInt()));
   m_dataBitsCb->setCurrentIndex(m_dataBitsCb->findText(settings.value("/serialsetting/Databits", "8").toString()));
   m_parityCb->setCurrentIndex(m_parityCb->findText(settings.value("/serialsetting/Parity", "None").toString()));
   m_stopCb->setCurrentIndex(m_stopCb->findText(settings.value("/serialsetting/Stopbits", "1").toString()));
   //m_protoPb->setCurrentIndex(m_protoPb->findText(settings.value("/serialsetting/Protocol", "Plain").toString()));

   bool hexoutput = settings.value("/serialsetting/HexOutput", false).toBool();
   m_hexOutputCb->setChecked(hexoutput);
   on_m_hexOutputCb_toggled(hexoutput);
   m_DatePrefixCb->setChecked(settings.value("/serialsetting/DatePrefix", false).toBool());
   m_TimePrefixCb->setChecked(settings.value("/serialsetting/TimePrefix", true).toBool());
   m_UnprintfCb->setChecked(settings.value("/serialsetting/UncharDataPrint", false).toBool());

   bool enablelog = settings.value("/serialsetting/EnableLogging",false).toBool();
   m_enableLoggingCb->setChecked(enablelog);
   m_logFileLe->setText(settings.value("/serialsetting/LogFileName", QDir::homePath()+"/serialsetting.log").toString());
   m_logAppendCb->setCurrentIndex(settings.value("/serialsetting/AppendToLogFile", 0).toInt());
   on_m_enableLoggingCb_toggled(enablelog);
        /*  input setting   */
    //m_appendReturnCb->setChecked(settings.value("/serialsetting/AppendReturnWhenSend", true).toBool());
    //m_charDelaySb->setValue(settings.value("/serialsetting/CharDelay", 1).toInt());
    //m_inputModeCb->setCurrentIndex(m_inputModeCb->findText(settings.value("/serialsetting/LineMode", "LF line end").toString()));

   bool entryFound = settings.contains("/serialsetting/AllDevices");
   QStringList devices=settings.value("/serialsetting/AllDevices").toStringList();
   if (!entryFound)
   {
#ifdef _TTY_POSIX_
      devices<<"/dev/ttyS0"<<"/dev/ttyS1"<<"/dev/ttyS2";
#endif
#ifdef _TTY_WIN_
      devices<<"com1"<<"com2"<<"com3";
#endif
   }
   m_deviceCb->insertItems(0, devices);
   entryFound = settings.contains("/serialsetting/CurrentDevice");
   QString curdev = settings.value("/serialsetting/CurrentDevice").toString();
   if(!entryFound)
   {
       curdev = devices.first();
   }

   m_deviceCb->setCurrentIndex(devices.indexOf(curdev));
}

void SetUpDlg::saveSettings()
{
   QSettings settings;
   settings.setValue("/serialsetting/HardwareHandshake", m_hardwareCb->isChecked());
   settings.setValue("/serialsetting/SoftwareHandshake", m_softwareCb->isChecked());

   settings.setValue("/serialsetting/OpenForWriting", m_writeCb->isChecked());
   //settings.setValue("/serialsetting/DontApplySettings", !m_applyCb->isChecked());

   settings.setValue("/serialsetting/Device", m_deviceCb->currentText());
   settings.setValue("/serialsetting/Baud", m_baudCb->itemData(m_baudCb->currentIndex()));

   settings.setValue("/serialsetting/Databits", m_dataBitsCb->currentText());
   settings.setValue("/serialsetting/Parity", m_parityCb->currentText());
   settings.setValue("/serialsetting/Stopbits", m_stopCb->currentText());
   //settings.setValue("/serialsetting/Protocol", m_protoPb->currentText());

   settings.setValue("/serialsetting/HexOutput", m_hexOutputCb->isChecked());
   settings.setValue("/serialsetting/DatePrefix", m_DatePrefixCb->isChecked());
   settings.setValue("/serialsetting/TimePrefix", m_TimePrefixCb->isChecked());
   settings.setValue("/serialsetting/UncharDataPrint", m_UnprintfCb->isChecked());
   /*   input setting      */
   //settings.setValue("/serialsetting/AppendReturnWhenSend", m_appendReturnCb->isChecked());
   //settings.setValue("/serialsetting/LineMode", m_inputModeCb->currentText());
   //settings.setValue("/serialsetting/CharDelay", m_charDelaySb->value());

   //settings.setValue("/serialsetting/SendFileDialogStartDir", m_sendFileDialogStartDir);
   settings.setValue("/serialsetting/EnableLogging" , m_enableLoggingCb->isChecked());
   settings.setValue("/serialsetting/LogFileName", m_logFileLe->text());
   settings.setValue("/serialsetting/AppendToLogFile", m_logAppendCb->currentIndex());

   QString currentDevice=m_deviceCb->currentText();
   settings.setValue("/serialsetting/CurrentDevice", currentDevice);
   bool currentDeviceIsInList=false;
   QStringList devices;
   for (int i=0; i<m_deviceCb->count(); i++)
   {
      QString s=m_deviceCb->itemText(i);
      devices<<s;
      if (s==currentDevice)
      {
         currentDeviceIsInList=true;
      }
   }

   if (!currentDeviceIsInList)
   {
      devices<<currentDevice;
   }

   settings.setValue("/serialsetting/AllDevices", devices);

}


void SetUpDlg::fillBaudCb()
{
#ifdef _TTY_POSIX_
   m_baudCb->addItem("50",BAUD50);
   m_baudCb->addItem("75",BAUD75);
#endif

   m_baudCb->addItem("110",BAUD110);

#ifdef _TTY_POSIX_
   m_baudCb->addItem("134",BAUD134);
   m_baudCb->addItem("150",BAUD150);
   m_baudCb->addItem("200",BAUD200);
#endif

   m_baudCb->addItem("300",BAUD300);

   m_baudCb->addItem("600",BAUD600);

   m_baudCb->addItem("1200",BAUD1200);

#ifdef _TTY_POSIX_
   m_baudCb->addItem("1800",BAUD1800);
#endif

   m_baudCb->addItem("2400",BAUD2400);

   m_baudCb->addItem("4800",BAUD4800);

   m_baudCb->addItem("9600",BAUD9600);

#ifdef _TTY_WIN_
   m_baudCb->addItem("14400",BAUD14400);
#endif

   m_baudCb->addItem("19200",BAUD19200);

   m_baudCb->addItem("38400",BAUD38400);

#ifdef _TTY_WIN_
   m_baudCb->addItem("57600",BAUD56000);
#endif
   m_baudCb->addItem("57600",BAUD57600);

#ifdef _TTY_POSIX_
   m_baudCb->addItem("76800",BAUD76800);
#endif

   // this one is the default (without special reason)
   m_baudCb->addItem("115200",BAUD115200);
   m_baudCb->setCurrentIndex(m_baudCb->count()-1);

#ifdef _TTY_WIN_
   m_baudCb->addItem("128000",BAUD128000);
   m_baudCb->addItem("256000",BAUD256000);
#endif

}

void SetUpDlg::chooseLogFile()
{
   QString logFile=QFileDialog::getSaveFileName(this, tr("Save log file ..."), m_logFileLe->text());
   if (!logFile.isEmpty())
   {
      m_logFileLe->setText(logFile);
   }
}


void SetUpDlg::on_OkButtonBox_accepted()
{
     saveSettings();
}

void SetUpDlg::on_OkButtonBox_rejected()
{

}
/*
void SetUpDlg::on_m_applyCb_toggled(bool checked)
{
    m_baudCb->setEnabled(checked);
    m_dataBitsCb->setEnabled(checked);
    m_parityCb->setEnabled(checked);
    m_stopCb->setEnabled(checked);
    m_softwareCb->setEnabled(checked);
    m_hardwareCb->setEnabled(checked);
}
*/
void SetUpDlg::on_m_hexOutputCb_toggled(bool checked)
{
    m_TimePrefixCb->setEnabled(!checked);
    m_DatePrefixCb->setEnabled(!checked);
    m_UnprintfCb->setEnabled(!checked);
}

void SetUpDlg::on_m_enableLoggingCb_toggled(bool checked)
{
    m_logAppendCb->setEnabled(checked);
    m_logFileLe->setEnabled(checked);
    m_logFileFileDialog->setEnabled(checked);
}

void SetUpDlg::on_m_logAppendCb_currentIndexChanged(int index)
{

}
